//
//  ImageCell.m
//  HWFinal
//
//  Created by student on 2021/12/10.
//  Copyright © 2021 demo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ImageCell.h"


@interface ImgCell ()

@property(strong, nonatomic) NSString* url;//当前图片或视频的url
@property(strong, nonatomic) UIImage* loadImg;//表示加载过程中的图片
@property(strong, nonatomic) UIImageView* iv;//图片框架

@property(strong, nonatomic) ModelController* mc;

@end

@implementation ImgCell

//初始化。若type=0是图片，则异步加载图片；若type=1是视频，则异步加载视频缩略图。异步加载过程都是先在本地缓存中寻找，再进行加载。
-(void)setImgWithURL: (NSString*)url
             andType: (int)type
{
    //初始化
    self.mc = [ModelController getInstance];
    self.loadImg = [UIImage imageNamed:@"loadimg.png" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
    self.url = url;
    
    //添加图片框架，默认先是加载图片
    self.iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    self.iv.image = self.loadImg;
    self.iv.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:self.iv];
    
    //图片
    if(type == 0){
        self.iv.contentMode = UIViewContentModeScaleAspectFill;
        self.iv.clipsToBounds = YES;
        
        //先从缓存中加载
        self.img = [self.mc loadImageFromCache:self.url];
        if(self.img != nil){
            self.iv.image = self.img;
        }
        else{
            //异步从网络加载，成功后通知主线程
            NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(loadImageFromWeb) object:nil];
            NSOperationQueue* queue = [[NSOperationQueue alloc]init];
            [queue addOperation:op];
        }
    }
    //视频
    else{
        self.iv.contentMode = UIViewContentModeCenter;
        self.iv.clipsToBounds = YES;
        
        //添加视频的播放标志
        UIImageView* subiv = [[UIImageView alloc]initWithFrame:CGRectMake(self.bounds.size.width / 2 - 30, self.bounds.size.height / 2 - 30, 60, 60)];
        subiv.layer.cornerRadius = 30;
        subiv.backgroundColor = [UIColor lightGrayColor];
        subiv.image = [UIImage imageNamed:@"bofang.png" inBundle:[NSBundle mainBundle] compatibleWithTraitCollection:nil];
        [self.iv addSubview:subiv];
        
        //先从缓存中加载
        NSString* filePath = [self.mc loadVideoFromCache:self.url];
        if(filePath != nil){
            self.img = [self.mc getRawImg:[NSURL fileURLWithPath:filePath]];
            self.iv.image = self.img;
        }
        else{
            //异步从网络加载，成功后通知主线程
            NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(loadVideoFromWeb) object:nil];
            NSOperationQueue* queue = [[NSOperationQueue alloc]init];
            [queue addOperation:op];
        }
    }
}

-(void)loadImageFromWeb
{
    //异步加载结束后要通知主线程
    NSURL* photoURL = [NSURL URLWithString:self.url];
    self.img = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoURL]];
    dispatch_sync(dispatch_get_main_queue(), ^{
        self.iv.image = self.img;
    });
    
    
    //网络加载完成后，下载到本地缓存
    [self.mc downloadFileToCache:self.url];
}

-(void)loadVideoFromWeb
{
    //异步加载结束后要通知主线程
    NSURL* videoURL = [NSURL URLWithString:self.url];
    self.img = [self.mc getRawImg:videoURL];
    dispatch_sync(dispatch_get_main_queue(), ^{
        self.iv.image = self.img;
    });
    
    //网络加载完成后，下载到本地缓存
    [self.mc downloadFileToCache:self.url];
}

@end
